{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'self' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m/tmp/ipykernel_2379022/2222623196.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      9\u001b[0m         \u001b[0mport\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mparams\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     10\u001b[0m         \u001b[0;31m# print(pid, ip, port)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 11\u001b[0;31m         \u001b[0;32mif\u001b[0m \u001b[0mpid\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshard\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     12\u001b[0m             \u001b[0;32mcontinue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     13\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mpid\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mid\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mNameError\u001b[0m: name 'self' is not defined"
     ]
    }
   ],
   "source": [
    "addresse = []\n",
    "\n",
    "with open('hosts.config', 'r') as hosts:\n",
    "    for line in hosts:\n",
    "        params = line.split()\n",
    "        pid = int(params[0])\n",
    "        priv_ip = params[1]\n",
    "        pub_ip = params[2]\n",
    "        port = int(params[3])\n",
    "        # print(pid, ip, port)\n",
    "        if pid not in range(self.shard):\n",
    "            continue\n",
    "        if pid == self.id:\n",
    "            my_address = (priv_ip, port)\n",
    "        # for _ in range((test_tatget*self.shard), (test_tatget+1)*self.shard):\n",
    "        for _ in range(0,4):\n",
    "            addresses[pid] = (pub_ip, port)\n",
    "\n",
    "print(addresses)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hosts.config is correctly read\n",
      "[('127.0.0.1', 10007), ('127.0.0.1', 10107), ('127.0.0.1', 10207), ('127.0.0.1', 10307)]\n",
      "('127.0.0.1', 10007)\n"
     ]
    }
   ],
   "source": [
    "N = 4\n",
    "addresses = [None] * N\n",
    "try:\n",
    "    with open('arbc_hosts.config', 'r') as hosts:\n",
    "        for line in hosts:\n",
    "            params = line.split()\n",
    "            pid = int(params[0])\n",
    "            priv_ip = params[1]\n",
    "            pub_ip = params[1]\n",
    "            port = int(params[2])\n",
    "            # print(pid, ip, port)\n",
    "            if pid not in range(N):\n",
    "                continue\n",
    "            if pid == 0:\n",
    "                my_address = (priv_ip, port)\n",
    "            addresses[pid] = (pub_ip, port)\n",
    "    assert all([node is not None for node in addresses])\n",
    "    print(\"hosts.config is correctly read\")\n",
    "except FileNotFoundError or AssertionError as e:\n",
    "    traceback.print_exc()\n",
    "\n",
    "print(addresses)\n",
    "print(my_address)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "this is the addresses: [('127.0.0.1', 10000), ('127.0.0.1', 10200), ('127.0.0.1', 10400), ('127.0.0.1', 10600), ('127.0.0.1', 10800), ('127.0.0.1', 11000), ('127.0.0.1', 11200), ('127.0.0.1', 11400), ('127.0.0.1', 11600), ('127.0.0.1', 11800), ('127.0.0.1', 12000), ('127.0.0.1', 12200), ('127.0.0.1', 12400), ('127.0.0.1', 12600), ('127.0.0.1', 12800), ('127.0.0.1', 13000), ('127.0.0.1', 13200), ('127.0.0.1', 13400), ('127.0.0.1', 13600), ('127.0.0.1', 13800), ('127.0.0.1', 14000), ('127.0.0.1', 14200), ('127.0.0.1', 14400), ('127.0.0.1', 14600), ('127.0.0.1', 14800), ('127.0.0.1', 15000), ('127.0.0.1', 15200), ('127.0.0.1', 15400), ('127.0.0.1', 15600), ('127.0.0.1', 15800), ('127.0.0.1', 16000), ('127.0.0.1', 16200)]\n"
     ]
    }
   ],
   "source": [
    "import traceback\n",
    "\n",
    "\n",
    "test_tatget = 1 # test shadr id 0\n",
    "shard = 4\n",
    "id =  0 \n",
    "N = 32\n",
    "\n",
    "        # address is the id of targett shard\n",
    "addresses = [None] * N\n",
    "try:\n",
    "    with open('hosts.config', 'r') as hosts:\n",
    "        for line in hosts:\n",
    "            params = line.split()\n",
    "            pid = int(params[0])\n",
    "            priv_ip = params[1]\n",
    "            pub_ip = params[1]\n",
    "            port = int(params[3])\n",
    "            # print(pid, ip, port)\n",
    "            if pid not in range(N):\n",
    "                continue\n",
    "            if pid == id:\n",
    "                my_address = (priv_ip, port)\n",
    "            # for i in range((test_tatget*shard), (test_tatget+1)*shard):\n",
    "            for _ in range(N):\n",
    "                addresses[pid] = (pub_ip, port)\n",
    "    # assert all([node is not None for node in addresses])\n",
    "except FileNotFoundError or AssertionError as e:\n",
    "    traceback.print_exc()\n",
    "\n",
    "print('this is the addresses:',addresses)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4 127.0.0.1 10800\n",
      "5 127.0.0.1 11000\n",
      "6 127.0.0.1 11200\n",
      "7 127.0.0.1 11400\n",
      "[('127.0.0.1', 10800), ('127.0.0.1', 11000), ('127.0.0.1', 11200), ('127.0.0.1', 11400)]\n"
     ]
    }
   ],
   "source": [
    "from pytest import param\n",
    "\n",
    "\n",
    "shard = 4\n",
    "add_list = [None] * shard\n",
    "# add_list = []\n",
    "\n",
    "try:\n",
    "    with open('hosts.config', 'r') as hosts:\n",
    "        lines = hosts.readlines() \n",
    "        for address in lines[4:8]:\n",
    "            params = address.split()\n",
    "            # print(params)\n",
    "            id = int(params[0])\n",
    "            pub_ip = params[1]\n",
    "            port = int(params[3])\n",
    "            print(id,pub_ip,port)\n",
    "            # for _ in range(shard):\n",
    "            # add_list.append(id)\n",
    "            add_list[id%4] = (pub_ip, port)\n",
    "\n",
    "except FileNotFoundError or AssertionError as e:\n",
    "    traceback.print_exc()\n",
    "\n",
    "print(add_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hosts.config is correctly read\n",
      "[('127.0.0.1', 10000), ('127.0.0.1', 10200), ('127.0.0.1', 10400), ('127.0.0.1', 10600), ('127.0.0.1', 10800), ('127.0.0.1', 11000), ('127.0.0.1', 11200), ('127.0.0.1', 11400)]\n"
     ]
    }
   ],
   "source": [
    "MR = 8\n",
    "N = 4\n",
    "i = 0\n",
    "addresses = [None] * MR\n",
    "try:\n",
    "    with open('hosts.config', 'r') as hosts:\n",
    "        for line in hosts:\n",
    "            params = line.split()\n",
    "            pid = int(params[0])\n",
    "            priv_ip = params[1]\n",
    "            pub_ip = params[2]\n",
    "            port = int(params[3])\n",
    "            # print(pid, ip, port)\n",
    "            if (pid) not in range(MR):\n",
    "                continue\n",
    "            if pid == i:\n",
    "                my_address = (priv_ip, port)\n",
    "            addresses[pid] = (pub_ip, port)\n",
    "    assert all([node is not None for node in addresses])\n",
    "    print(\"hosts.config is correctly read\")\n",
    "except FileNotFoundError or AssertionError as e:\n",
    "    traceback.print_exc()\n",
    "\n",
    "print(addresses)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.7.13 ('hbb': conda)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.13"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "7f5c99d094b399fa9e65cc4107643c6d5293cb8e1c21c8f7db515a0780c667bb"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
